//
//  DataBaseHandle.m
//  UI18_DataBeats
//
//  Created by QQn1993 on 15/11/18.
//  Copyright (c) 2015年 QQn1993. All rights reserved.
//

#import "DataBaseHandle.h"
//#import "Student.h"
#import "collect.h"
#import <sqlite3.h>

@interface DataBaseHandle ()

@property (nonatomic, copy) NSString *dataBasePath;

@end

@implementation DataBaseHandle

//获取数据库保存的路径

//定义全局，只保证存一个
//static DataBaseHandle *databaseHandle = nil;

- (NSString *)dataBasePath
{
    if (!_dataBasePath) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [paths lastObject];
        self.dataBasePath = [path stringByAppendingString:@"/student.sqlite"];
    }
    return _dataBasePath;
}

+ (instancetype) shareDataBase
{
//    以这种方法保证整个工程无论什么时候调用，都会返回相同的databaseHandle
    static DataBaseHandle *data = nil;
    if (data == nil) {
        data = [[DataBaseHandle alloc] init];
    }
    return data;
}

// 数据库指针，
static sqlite3 *db = nil ;

//打开数据库

- (void)openDataBase{
//    打印路径
    NSLog(@"%@",self.dataBasePath);
    
    int result = sqlite3_open(self.dataBasePath.UTF8String, &db);
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }
    else{
        NSLog(@"数据库打开失败   %d" , result);
    }
}

//关闭数据库
- (void) closeDatabase
{
    int result = sqlite3_close(db);
    
    if (result == SQLITE_OK) {
        NSLog(@"关闭数据库成功");
    }
    else{
        NSLog(@"关闭数据库失败  %d",result);
    }
}


#pragma mark 创建套餐或案例收藏

- (void)createMealsTable
{
    NSString *sqlstring = @"create table if not exists Meals ( ID text primary key  , name text, type text , img text)" ;
    
    int result = sqlite3_exec(db, sqlstring.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"建表成功");
    }
    else{
        NSLog(@"建表失败  %d",result) ;
    }
}

- (void)insertMealsID:(NSString *)ID AndName:(NSString *)name AndType:(NSString *)type AndImg:(NSString *)img
{
    NSString *sqlstring = @"insert into Meals (ID , name , type , img) values (?,?,?,?)";
    
    sqlite3_stmt *stmt = NULL;
    
    int result = sqlite3_prepare(db, sqlstring.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        
        sqlite3_bind_text(stmt, 1, ID.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 2, name.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 3, type.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 4, img.UTF8String, -1, NULL);
        
        if (sqlite3_step(stmt) == SQLITE_DONE) {
            NSLog(@"zheju插入成功");
        }
        else{
            NSLog(@"插入失败");
        }
    }
    else{
        NSLog(@"**插入语句错误");
    }
    
    sqlite3_finalize(stmt);
}

- (NSArray *)selectMealsArray:(NSString *)type
{
    NSLog(@"%@",type);
    NSString *sqlsreing = @"select* from Meals where type = ?";
    
    //    2.获取伴随指针
    sqlite3_stmt *stmt = NULL;
    //    3.预执行
    int result = sqlite3_prepare(db, sqlsreing.UTF8String, -1, &stmt, NULL);
        //    4.绑定参数
    sqlite3_bind_text(stmt, 1, type.UTF8String, -1, NULL);
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
    
    if (result == SQLITE_OK) {
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //            从伴随指针获取数据
            NSString *ID = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
            NSString *name = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 1)];
            NSString *type = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 2)];
            NSString *img = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 3)];
            
            collect *collec = [[collect alloc] init];
            collec.ID = ID;
            collec.name = name;
            collec.type = type;
            collec.img = img;
            
            [array addObject:collec];
        }
        NSLog(@"返回数组成功");
    }
    else{
        NSLog(@"**返回数组语法错误");
    }
    sqlite3_finalize(stmt);
    return array;
}

- (NSString *)selectMealsDressWhereId:(NSString *)ID
{
    //    1.准备sql语句
    NSString *sqlsreing = @"select name from Meals where ID = ?";
    
    NSString *dress = [NSString string];
    
    //    2.获取伴随指针
    sqlite3_stmt *stmt = NULL;
    //    3.预执行
    int result = sqlite3_prepare(db, sqlsreing.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        //    4.绑定参数
        sqlite3_bind_text(stmt, 1, ID.UTF8String, -1, NULL);
        //    5.执行
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //        5.1 从伴随指针获取数据
            dress = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
        }
    }
    else{
        NSLog(@"语法错误");
    }
    //    6.关闭伴随指针
    sqlite3_finalize(stmt);
    return dress;
}

- (void)deleteMealsWhereID:(NSString *)ID
{
    //    1.准备sql语句
    NSString *sqltring = @"delete from Meals where ID = ?";
    //    2.获取伴随指针
    sqlite3_stmt *stmt = NULL;
    //    3.预执行
    int result = sqlite3_prepare(db, sqltring.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        //        4.绑定参数
        sqlite3_bind_text(stmt, 1, ID.UTF8String, -1, NULL);
        //        5.执行
        if (sqlite3_step(stmt) == SQLITE_DONE) {
            NSLog(@"删除成功");
        }
        else{
            NSLog(@"删除失败");
        }
    }
    else{
        NSLog(@"语法错误");
    }
    //    6.关闭伴随指针
    sqlite3_finalize(stmt);
}

/*
- (Student *) selectvalueNumber:(NSString *)number
{
    //    1.准备sql语句
    NSString *sqlsreing = @"select name,phoneNumber from students where phoneNumber = ?";
    //    2.获取伴随指针
    sqlite3_stmt *stmt = NULL;
    //    3.预执行
    int result = sqlite3_prepare(db, sqlsreing.UTF8String, -1, &stmt, NULL);
    Student *stu = [[Student alloc] init];
    if (result == SQLITE_OK) {
        //    4.绑定参数
        sqlite3_bind_text(stmt, 1, number.UTF8String, -1, NULL);
        //    5.执行
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //        5.1 从伴随指针获取数据
            NSString *selectName = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
            NSString *selectNumber = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 1)];
            stu.name = selectName;
            stu.phoneNumber = selectNumber;
            //        NSLog(@"%@   %@",selectName,selectNumber);
        }
    }
    else{
        NSLog(@"语法错误");
    }
    //    6.关闭伴随指针
    sqlite3_finalize(stmt);
    
    return stu;

}*/

//删除

/*

- (void) deletStuWhereNumber:(NSString *)number
{
//    1.准备sql语句
    NSString *sqltring = @"delete from students where phoneNumber = ?";
//    2.获取伴随指针
    sqlite3_stmt *stmt = NULL;
//    3.预执行
    int result = sqlite3_prepare(db, sqltring.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
//        4.绑定参数
        sqlite3_bind_text(stmt, 1, number.UTF8String, -1, NULL);
//        5.执行
        if (sqlite3_step(stmt) == SQLITE_DONE) {
            NSLog(@"删除成功");
        }
        else{
            NSLog(@"删除失败");
        }
    }
    else{
        NSLog(@"语法错误");
    }
    //    6.关闭伴随指针
    sqlite3_finalize(stmt);
}*/

@end
